package models
{
	
	import game.bejeweled.Ball;
	

	public class BallPool
	{
		private var pool:Vector.<Ball>=new Vector.<Ball>(); 
		public function BallPool()
		{
		}
		
		/**
		 * 获取对象  
		 * @return 
		 * 
		 */		
		public function getClass(ballModale:BallModel):Ball{
//				trace("pool.length="+pool.length)
			if(pool.length>0){
				var ball:Ball=pool.shift();
				ball.texture=ballModale.ballTexture;
				ball.ballProfile=ballModale;
				//
				ball.alpha=1;
				//
				return ball;
			}
			ball=new Ball(ballModale.ballTexture);
			ball.ballProfile=ballModale;
			return ball;
		}
		/**
		 * 归还对象 
		 * @param cls
		 * @return 
		 * 
		 */		
		public function returnClass(ball:Ball):Boolean
		{
//			trace("pool.length2="+pool.length)
			var index:int=pool.indexOf(ball);
			if(index==-1){
				ball.reset();
				pool.push(ball);
				return true;
			}
			return false;	
		}
		
		
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:BallPool;
		public static function get instant():BallPool
		{
			if(null == _instant)
			{
				_instant =  new BallPool();
			}
			return _instant;
		}
	}
}